package com.magicalcoder.youyaboot.admin.api.survey;

import com.magicalcoder.youyaboot.core.service.CommonRestController;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.math.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import com.magicalcoder.youyaboot.core.common.constant.PageConstant;
import com.magicalcoder.youyaboot.core.common.exception.BusinessException;
import com.magicalcoder.youyaboot.core.serialize.ResponseMsg;
import com.magicalcoder.youyaboot.model.Survey;
import com.magicalcoder.youyaboot.service.survey.service.SurveyService;

import com.magicalcoder.youyaboot.core.utils.ListUtil;
import com.magicalcoder.youyaboot.core.utils.MapUtil;
import com.magicalcoder.youyaboot.core.utils.StringUtil;


/**
* 代码为自动生成 Created by www.magicalcoder.com
* 软件作者：何栋宇 qq:709876443
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:648595928
*/

@RequestMapping("/admin/survey_rest/")
@RestController
public class AdminSurveyRestController extends CommonRestController<Survey,Integer> implements InitializingBean
{

    @Resource
    private SurveyService surveyService;

        //分页查询
    @RequestMapping(value={"page"}, method={RequestMethod.GET})
    public ResponseMsg page(
        @RequestParam(required = false,value ="idFirst")                            Integer idFirst ,
        @RequestParam(required = false,value ="reportnumFirst")                            String reportnumFirst ,
        @RequestParam(required = false,value ="companyFirst")                            String companyFirst ,
        @RequestParam(required = false,value ="GuaranreeIdFirst")                            String GuaranreeIdFirst ,
        @RequestParam(required = false,value ="licensePlateNumberFirst")                            String licensePlateNumberFirst ,
        @RequestParam(required = false,value ="TroubleDriverNameFirst")                            String TroubleDriverNameFirst ,
        @RequestParam(required = false,value ="guaranteeIdFirst")                            String guaranteeIdFirst ,
        @RequestParam int page,@RequestParam int limit,@RequestParam(required = false) String safeOrderBy
        ,HttpServletResponse response,@RequestParam(required = false) Integer queryType
    ){
        Map<String,Object> query = new HashMap();
        query.put("idFirst",idFirst);
        query.put("reportnumFirst",coverBlankToNull(reportnumFirst));
        query.put("companyFirst",coverBlankToNull(companyFirst));
        query.put("GuaranreeIdFirst",coverBlankToNull(GuaranreeIdFirst));
        query.put("licensePlateNumberFirst",coverBlankToNull(licensePlateNumberFirst));
        query.put("TroubleDriverNameFirst",coverBlankToNull(TroubleDriverNameFirst));
        query.put("guaranteeIdFirst",coverBlankToNull(guaranteeIdFirst));
        Integer count = surveyService.getModelListCount(query);
        if(StringUtil.isBlank(safeOrderBy)){
            query.put("notSafeOrderBy","id desc");
        }else{
            query.put("safeOrderBy",safeOrderBy);
        }
        if(queryType==null || queryType == QUERY_TYPE_SEARCH){//普通查询
            limit = Math.min(limit, PageConstant.MAX_LIMIT);
            query.put("start",(page - 1) * limit);query.put("limit",limit);
            return new ResponseMsg(count,surveyService.getModelList(query));
        }else if(queryType == QUERY_TYPE_EXPORT_EXCEL){
            query.put("start",(page - 1) * limit);query.put("limit",limit);
            exportExcel(response,surveyService.getModelList(query),"勘查表",
            new String[]{"查勘编号","报案编号","单位","保单号","车牌号","发动机号","车架号","厂牌型号","肇事司机姓名","肇事司机电话","准驾车型","承保交强险公司","交强险保单号","三责厂牌型号","三责车牌号","三责司机姓名","肇事时间","肇事地点","查勘时间","查勘地点","是否第一现场","是否第二现场","事故责任","涉及险种","保险责任","处理意见","损失部位","其他损失","人员伤亡情况","所住医院","科室","入院时间","车辆损失金额","三责损失金额","其他损失金额","合计","事故照片","查勘人员","录入时间","录入人员","状态"},
            new String[]{"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""});
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.commonService = surveyService;
        super.primaryKey = "id";//硬编码此实体的主键名称
    }
}
